package com.acoffee.controller;

import com.acoffee.pojo.Dto;
import com.acoffee.pojo.RbacManager;
import com.acoffee.pojo.ResponseEntity;
import com.acoffee.service.RbacManagerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author acoffee
 * @create 2021-11-25 18:47
 */
@RestController
public class RbacManagerController {

    @Autowired
    RbacManagerService rbacManagerService;

//    @RequestMapping("/queryAll")
//    public List<RbacManager> queryAll(){
//        List<RbacManager> rbacManagers = rbacManagerService.queryAll();
//        return rbacManagers;
//    }

    @PostMapping("/queryAllByPage")
    public ResponseEntity<PageInfo<RbacManager>> queryAllByPage(@RequestBody Dto dto){
        PageHelper.startPage(dto.getPage(),dto.getLimit());
        List<RbacManager> managers = rbacManagerService.queryAll(dto);
        PageInfo<RbacManager> managerPageInfo = new PageInfo<>(managers);
        return new ResponseEntity(200,"查询成功",managerPageInfo);
    }
//    @GetMapping("/queryAllByPage/{startPage}/{pageSize}")
//    public ResponseEntity<PageInfo<RbacManager>> queryAllByPage(@PathVariable("startPage") int startPage,@PathVariable("pageSize")int pageSize){
//        PageHelper.startPage(startPage,pageSize);
//        List<RbacManager> managers = rbacManagerService.queryAll();
//        PageInfo<RbacManager> managerPageInfo = new PageInfo<>(managers);
//        return new ResponseEntity(200,"查询成功",managerPageInfo);
//    }

    @GetMapping("/queryOne/{id}")
    public ResponseEntity<RbacManager> queryOne(@PathVariable int id){
        RbacManager manager = rbacManagerService.queryOne(id);
        return new ResponseEntity(200,"查询成功",manager);
    }

    @PutMapping("/addOne")
    public ResponseEntity addOne(@RequestBody RbacManager manager){
        ResponseEntity<RbacManager> entity = new ResponseEntity<>();
        boolean flag = rbacManagerService.addOne(manager);
        if(flag){
            entity.setCode(200);
            entity.setMsg("添加成功！");
            entity.setData(manager);
        }else{
            entity.setCode(303);
            entity.setMsg("添加失败！");
        }
        return entity;
    }

    @PostMapping("/updateOne")
    public ResponseEntity updateOne(@RequestBody RbacManager manager){
        ResponseEntity<RbacManager> entity = new ResponseEntity<>();
        boolean flag = rbacManagerService.updateOne(manager);
        if(flag){
            entity.setCode(200);
            entity.setMsg("修改成功！");
            entity.setData(manager);
        }else{
            entity.setCode(303);
            entity.setMsg("修改失败！");
        }
        return entity;
    }

    @DeleteMapping("/deleteOne/{id}")
    public ResponseEntity deleteOne(@PathVariable int id){
        System.out.println(1111);
        ResponseEntity<Object> entity = new ResponseEntity<>();
        boolean flag = rbacManagerService.deleteOne(id);

        if(flag){
            entity.setCode(200);
            entity.setMsg("删除成功！");
        }else{
            entity.setCode(303);
            entity.setMsg("删除失败！");
        }
        return entity;
    }

}
